#pragma once
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <string>
#include <vector>
#include <functional>
#include <stdio.h>
#include <queue>
#include "log.hpp"
using namespace std;

using fun = function<void(void*)>;

class mypthread
{
public:
    static void *pthread_function(void *formal)
    {
        mypthread *tmp = static_cast<mypthread*>(formal);
        tmp->work();
        return nullptr;
    }

    string name()
    {
        return _name;
    }

    void work()
    {
        _fun(_this);
    }

    mypthread(string name, fun function, void* _pf) : _name(name), _run(false), _fun(function), _this(_pf)
    {
    }

    ~mypthread()
    {
    }

    bool start()
    {
        sleep(1);
        int i = pthread_create(&_id, nullptr, pthread_function, this);
        cout << _name << endl;
        if (i == 0)
        {
            _run = true;
        }
        else
        {
            return false;
        }
        return true;
    }

    void join()
    {
        if (_run)
        {
            pthread_join(_id, nullptr);
        }
    }

    void Detach()
    {
        if (_run)
        {
            pthread_detach(_id);
        }
    }

    void stop()
    {
        _run = false;
    }



private:
    string _name;
    pthread_t _id;
    bool _run;
    fun _fun;
    void* _this;
};

